﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MoneyWentWhere.DataModel.Reports
{
    public class CategoryLevelReport : TableReport
    {
        readonly int CategoryLevel;

        public CategoryLevelReport(TimeSpanUnit periodUnit, int categoryLevel)
            : base(periodUnit) 
        {
            CategoryLevel = categoryLevel;        
        }

        protected override TableReportColumn CreateColumn(PeriodAmount pa)
        {
            return new TableReportColumn(
                pa.Key.Account,
                pa.Key.Kind,
                IsDepositOnlyForNormal(pa),
                GetCategoryPath(pa.Key.Category, CategoryLevel));
        }

        bool IsDepositOnlyForNormal(PeriodAmount pa)
        {
            if (pa.Key.Kind == TransactionKind.Normal) { return pa.Key.IsDeposit; }
            else { return true; }
        }

        static String GetCategoryPath(String fullCat, int level)
        {
            if (String.IsNullOrEmpty(fullCat) || level == 0) { return String.Empty; }

            String[] levs = fullCat.Split('/');

            StringBuilder sb = new StringBuilder();
            int count = Math.Min(level, levs.Length);
            for (int i = 0; i < count; i++)
            {
                sb.Append(levs[i]);
                if (i < count - 1) { sb.Append('/'); }
            }
            return sb.ToString();
        }

    }
}
